package org.anupta.game.utils.others;

import java.util.Random;

public class Utils {
	private static final Random random = new Random();
    private static final char[] hash = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
        'o', 'p', 'q', 'r', 's','t', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
        'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U','V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3',
        '4', '5', '6', '7', '8', '9'};
    private static final char[] specialChar = {'!','@','#','$','%','?','&','*','(',')','\'','\"','-','_'};
	
    public static StringBuilder getRandomString(int length) {
        StringBuilder builder = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            builder.append(hash[random.nextInt(64)]);
        }
        return builder;
    }

    public static String getCryptedPassword(String key, String password){
        StringBuilder crypt = new StringBuilder();
        for (byte i = 0; i < password.length(); i++){
            char pass = password.charAt(i);
            char pKey = key.charAt(i);
            int aPass = pass >> 4;
            byte aKey = (byte)(pass % 16);
            byte anb = (byte)((aPass + pKey) % 64);
            byte anb2 = (byte)((aKey + pKey) % 64);
            crypt.append(hash[anb]);
            crypt.append(hash[anb2]);
        }
        return crypt.toString();
    }
    
    public static int getLastLetterIndex(String text){
    	int max = 0;
    	StringBuilder build = new StringBuilder();
    	build.append(hash).append(specialChar);
    	for(int i = 0; i < text.length(); i++){
    		char c = text.charAt(i);
    		if(build.toString().contains(c+"")){
    			if(text.lastIndexOf(c+"") > max){
    				max = text.lastIndexOf(c+"");
    			}else{
    				continue;
    			}
    		}else{
    			continue;
    		}
    	}
    	return max;
    }
    
    public static String removeCarriageReturn(String texte){
    	StringBuilder text = new StringBuilder(texte);
    	for(int i = 0; i < text.toString().getBytes().length; i++){
    		byte b = text.toString().getBytes()[i];
    		if(b == 13){
    			text.deleteCharAt(i);
    		}
    	}
    	return text.toString();
    }
}
